-- auto_cast
-- Created by chengb Mar/24/2016
-- 每若干轮自动施法攻击敌人（仅限玩家使用）

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 每回合触发
    trigger = function(who, prop, arg)
        -- 必须是玩家
        assert(who.type == OBJECT_TYPE_USER);

        -- 有无敌状态
        if #PropM.fetchProps(who, "invincible") > 0 then
            return false;
        end

        local cycle   = tonumber(prop[2]);
        local skillId = tonumber(prop[3]);
        local round = CombatM.getRound();
        local startRound = 0;

        if (round - startRound) % cycle ~= 0 then
            -- 不是攻击的回合
            return false;
        end

        -- 查找目标
        local target;
        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local grid = DungeonM.getGridByPos(p);

            -- 必须是未死亡的怪物
            if grid:isOpened() and
               grid:isMonster() and
               not grid.monster:isAudience() and
               not grid.monster:isDead() then
                target = grid.monster;
                break;
            end
        end

        -- 已经没有目标了，不处理
        if not target then
            return false;
        end

        local msg = string.format("[auto_cast]玩家自动施放技能(%d)攻击(%s)",
            skillId, target:getName());
        DungeonLogM.addLog(msg);

        -- 施放技能
        SkillM.apply(who, target, skillId, round);

        return true;
    end,

    record = function(target, prop, path)
        return false;
    end
}